package cn.wdm.user.service.impl;

import cn.wdm.basic.constants.BaseConstant;
import cn.wdm.basic.dto.RegisterDto;
import cn.wdm.basic.exception.BusinesExceprion;
import cn.wdm.basic.util.MD5Utils;
import cn.wdm.basic.util.StrUtils;
import cn.wdm.user.domain.Logininfo;
import cn.wdm.user.domain.User;
import cn.wdm.user.mapper.LogininfoMapper;
import cn.wdm.user.mapper.UserMapper;
import cn.wdm.user.service.IUserService;
import cn.wdm.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laozhang
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 注册短信验证码校验
     * @param dto
     */
    @Override
    public void phoneRegister(RegisterDto dto) {
        //非空校验
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        if (StringUtils.isEmpty(phone)||StringUtils.isEmpty(smsCode)||StringUtils.isEmpty(password)||StringUtils.isEmpty(passwordRepeat)){
            throw new BusinesExceprion("内容不能为空！");
        }
        //两次输入的密码校验
        if (!password.equals(passwordRepeat)) {
            new BusinesExceprion("两次输入的验证码不对");
        }
        //短信验证码校验
        //1.从redis中获取存入的短信验证码 没有获取到说明短信验证码过期了
        //smsCodeInRedisValue 9527:648979864894
        Object smsCodeInRedisValue = redisTemplate.opsForValue().get(BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone);
        if (smsCodeInRedisValue == null) {
            throw new BusinesExceprion("你的短信验证码过期了");
        }
        //不为空 redis中的短信验证码不为空 判断两次的输入是否一致
        if (!smsCode.equalsIgnoreCase(smsCodeInRedisValue.toString().split(":")[0])) {
            throw new BusinesExceprion("你输入的短信验证码有误");
        }
        //保存user+logininfo
        //保存user  先保存loginInfo 因为user中要设置logininfo的id
        User user=dto2User(dto);
        //级联保存loginInfo
        Logininfo logininfo =user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    /**
     * 通过邮箱注册
     * @param dto
     */
    @Override
    public void emailRegister(RegisterDto dto) {
        String email = dto.getEmail();
        String emailCode = dto.getEmailCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        //1.非空校验
        if (StringUtils.isEmpty(email)||StringUtils.isEmpty(emailCode)||StringUtils.isEmpty(password)||StringUtils.isEmpty(passwordRepeat)) {
            throw new BusinesExceprion("参数不能为空");
        }
        //2.两次密码输入校验
        if (!password.equals(passwordRepeat)) {
            throw new BusinesExceprion("；两次密码不一致");
        }
        //3.邮箱验证码和你输的邮箱验证码校验
        Object emailCodeValueInRedis = redisTemplate.opsForValue().get(BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + email);
            //3.1 如果redis中的邮箱验证码为空说明已经过期了
        if (emailCodeValueInRedis == null) {
            throw new BusinesExceprion("你的邮箱验证码已经过期，请重新获取");
        }
        if (!emailCode.equalsIgnoreCase(emailCodeValueInRedis.toString().split(":")[0])) {
            throw new BusinesExceprion("你输入的邮箱验证码不对，请重新输入");
        }
        //4.先保存loginInfo 后保存User
        User user=emailDto2User(dto);
        //级联保存loginInfo
        Logininfo logininfo =user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    /**
     * 邮件dto转为user对象
     * @return
     */
    private User emailDto2User(RegisterDto dto) {
        User user = new User();
        user.setEmail(dto.getEmail());
        user.setUsername(dto.getEmail());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Password = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Password);
        return user;
    }

    /**
     * 通过spring提供的工具类 进行一个对象的属性拷贝
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//必须设置logininfo的类型 1表示用户 0表示员工
        return logininfo;
    }

    /**
     * 根据传递的参数创建一个user对象
     * @param dto
     * @return
     */
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setPhone(dto.getPhone());
        user.setUsername(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Password = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Password);
        return user;
    }

    /**
      user的CUD  都需要级联操作logininfo
     */
    @Override
    public void update(User user) {
        super.update(user);
    }
    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

}
